/**
 * 
 */
package trungsi.gea.photos.controller;

import java.io.ByteArrayInputStream;
import java.util.Date;
import java.util.List;

import net.sourceforge.stripes.action.HandlesEvent;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.integration.spring.SpringBean;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import trungsi.gea.photos.domain.Folder;
import trungsi.gea.photos.domain.Photo;
import trungsi.gea.photos.service.PhotoService;
import trungsi.gea.photos.util.JsonUtils;

/**
 * @author trungsi
 *
 */
@UrlBinding("/photo")
public class PhotoListActionBean extends BaseActionBean {

	private static Log log = LogFactory.getLog(PhotoActionBean.class);
	
	@SpringBean
	private PhotoService photoService;
	
	private Folder folder;
	private Photo photo;
	
	private Date after;
	private int limit = 10;
	
	public void setFolder(Folder folder) {
		this.folder = folder;
	}
	
	public Folder getFolder() {
		return folder;
	}
	
	public void setPhoto(Photo photo) {
		this.photo = photo;
	}
	
	public Photo getPhoto() {
		return photo;
	}
	
	public void setAfter(Date after) {
		this.after = after;
	}
	
	public void setLimit(int limit) {
		this.limit = limit;
	}
	
	@HandlesEvent("show")
	public Resolution show() {
		if (photo != null) {
			try {
				return new StreamingResolution("text/javascript", getJsonPhotoResponse().toString());
			} catch (Exception e) {
				log.error(e);
				return new StreamingResolution("text/javascript", "{succes : false, error : '" + e + "'}");
			}
		} else 
			return new StreamingResolution("text/javascript", "{succes : false, error : 'photo not found'}");
	}
	
	@HandlesEvent("list")
	public Resolution list() {
		List<Photo> photos = findPhotosByFolder();
		try {
			JSONObject json = getJsonPhotosResponse(photos);
			
			return new StreamingResolution("text/javascript", json.toString());
		} catch (Exception e) {
			log.error(e);
			return new StreamingResolution("text/javascript", "{succes : false, error : '" + e + "'}");
		}
	}

	/*private List<Photo> findAll() {
		if (folder != null) {
			return findPhotosByFolder();
		}
		return (after != null) ? photoService.findAll(after, limit) : photoService.findAll(limit);
	}*/

	private JSONObject getJsonPhotosResponse(List<Photo> photos)
			throws JSONException {
		JSONObject json = new JSONObject();
		json.put("success", true);
		json.put("photos", new JSONArray());
		for (Photo photo : photos) {
			json.append("photos", JsonUtils.toJson(photo));
		}
		
		json.put("total", photoService.countByFolder(folder));
		
		/*if (folder != null) {
			json.put("total", photoService.countByFolder(folder));
		} else {
			json.put("total", photoService.countAll());
		}*/
		if (!photos.isEmpty()) {
			json.put("after", "" + photos.get(photos.size()-1).getCreatedDate().getTime());
		}
		System.out.println(json.toString());
		return json;
	}

	@HandlesEvent("showPhoto")
	public Resolution showPhoto() {
		if (photo != null)
			return new StreamingResolution("", new ByteArrayInputStream(photo.getPhoto()));
		else 
			return new StreamingResolution("text/javascript", "{succes : false, error : 'photo not found'}");
	}
	
	@HandlesEvent("showThumbnail")
	public Resolution showThumbnail() {
		if (photo != null) {
			log("Photo.showThumbnail " + photo.getId() + "," + photo.getName());
			return new StreamingResolution("", new ByteArrayInputStream(photo.getThumbnail()));
		} else 
			return new StreamingResolution("text/javascript", "{succes : false, error : 'photo not found'}");
	}

	private void log(String msg) {
		log.info(getContext().getRequest().getSession().getAttribute("facebookUser") + msg);
	}
	@HandlesEvent("showSlideshow")
	public Resolution showSlideshow() {
		if (photo != null) {
			log("Photo.showSlideshow " + photo.getId() + "," + photo.getName());
			return new StreamingResolution("", new ByteArrayInputStream(photo.getSlideshow()));
		} else 
			return new StreamingResolution("text/javascript", "{succes : false, error : 'photo not found'}");
	}

	private List<Photo> findPhotosByFolder() {
		if (after != null) {
			return photoService.findAllByFolder(folder, after, limit);
		} else {
			return photoService.findAllByFolder(folder, limit);
		}
	}
	
	private JSONObject getJsonPhotoResponse() throws JSONException {
		JSONObject json = new JSONObject();
		json.put("success", true);
		json.put("photo", JsonUtils.toJson(photo));
		return json;
	}
}
